/*
 * CircleDocumentWindowController.java
 *
 * Created on November 4, 2006, 8:37 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package edu.umn.cs5115.scheduler.circle;

import edu.umn.cs5115.scheduler.framework.Document;
import edu.umn.cs5115.scheduler.framework.DocumentController;
import edu.umn.cs5115.scheduler.framework.KeyValueAdapter;
import edu.umn.cs5115.scheduler.framework.KeyValueCoding;
import edu.umn.cs5115.scheduler.framework.KeyValueObserver;
import edu.umn.cs5115.scheduler.framework.WindowController;
import edu.umn.cs5115.scheduler.framework.gui.Binder;
import edu.umn.cs5115.scheduler.framework.gui.ManagedText;
import edu.umn.cs5115.scheduler.framework.gui.ValueTransformer;

import java.text.DecimalFormat;
import java.text.FieldPosition;
import java.text.NumberFormat;
import java.text.ParsePosition;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

/**
 *
 * @author grant
 */
public class CircleDocumentWindowController extends WindowController
{
    // controls that we create on the window.
    private Label nameLabel;        private Text nameText;
    private Label radiusLabel;      private Text radiusText;
    private Label centerXLabel;     private Text centerXText;
    private Label centerYLabel;     private Text centerYText;
    private Label areaLabel;        private Text areaText;
    private Label colorRedLabel;    private Text colorRedText;
    private Label colorGreenLabel;  private Text colorGreenText;
    private Label colorBlueLabel;   private Text colorBlueText;
    
    /**
     * Creates a new instance of CircleDocumentWindowController
     */
    public CircleDocumentWindowController(CircleDocument document)
    {
        super(document);
        // create the interface!
        Shell window = new Shell(DocumentController.getSharedDocumentController().getDisplay());
        GridLayout layout = new GridLayout(2, false);
        layout.marginWidth = 20;
        layout.marginHeight = 20;
        layout.verticalSpacing = 8;
        window.setLayout(layout);
        
        NumberFormatTransformer doubleTransformer = new NumberFormatTransformer(new DecimalFormat("#.###"));
        NumberFormatTransformer integerTransformer = new NumberFormatTransformer(new DecimalFormat("#"));
        
        // as we create the text fields and they are bound, we don't want undos to be registered.
        document.getUndoManager().disableUndoRegistration();
        
        nameLabel = createLabel(window, "Name:");
        nameText = createText(window, document.getCircle(), Circle.NAME_KEY, ValueTransformer.IDENTITY_TRANSFORM, "Rename Circle");
        
        radiusLabel = createLabel(window, "Radius:");
        radiusText = createText(window, document.getCircle(), Circle.RADIUS_KEY, doubleTransformer, "Change Circle Size");
        
        centerXLabel = createLabel(window, "Center X:");
        centerXText = createText(window, document.getCircle(), Circle.CENTER_X_KEY, integerTransformer, "Change Circle X");
        
        centerYLabel = createLabel(window, "Center Y:");
        centerYText = createText(window, document.getCircle(), Circle.CENTER_Y_KEY, integerTransformer, "Change Circle Y");
        
        areaLabel = createLabel(window, "Area:");
        areaText = createText(window, document.getCircle(), Circle.AREA_KEY, doubleTransformer, "Change Circle Size");
        
        colorRedLabel = createLabel(window, "R:");
        colorRedText = createText(window, document.getCircle(), Circle.COLOR_RED_KEY, integerTransformer, "Change Circle Color");
        
        colorGreenLabel = createLabel(window, "G:");
        colorGreenText = createText(window, document.getCircle(), Circle.COLOR_GREEN_KEY, integerTransformer, "Change Circle Color");
        
        colorBlueLabel = createLabel(window, "B:");
        colorBlueText = createText(window, document.getCircle(), Circle.COLOR_BLUE_KEY, integerTransformer, "Change Circle Color");
        document.getUndoManager().enableUndoRegistration();
        
        setShouldCloseDocument(true);
        window.pack();
        setWindow(window);
    }
    
    private Label createLabel(Shell window, String text)
    {
        Label theLabel = new Label(window, SWT.RIGHT);
        GridData data = new GridData();
        data.verticalIndent = 1;
        theLabel.setLayoutData(data);
        theLabel.setText(text);
        return theLabel;
    }
    
    /**
     * Create a text field and add it to the window.
     * @param window The window the new Text will be added to
     * @param bindValueTo The object to bind the value of the new text to.
     * @param key The key on the new object to bind the value to.
     * @param transformer A valueTransformer to use on the binding to transform
     * @param actionName The name to assign to the action of editing the fields
     * (for undo)
     * values.
     */
    private Text createText(Shell window, KeyValueCoding bindValueTo, String key, ValueTransformer transformer, final String actionName)
    {
        Text text = new Text(window, SWT.SINGLE | SWT.BORDER);
        GridData data = new GridData(SWT.FILL, SWT.CENTER, true, false);
        data.horizontalIndent = 8;
        text.setLayoutData(data);
        ManagedText managedText = new ManagedText(text, getDocument());
        // Note:  I wrote a binder class to ease this.  If you find it easier to
        // get the notifications manually, you can do that instead, too.
        new Binder(bindValueTo, key, managedText, ManagedText.TEXT_KEY, transformer);
        
        // This will be sure to set the action name when the text changes.
        managedText.addListener(new KeyValueAdapter()
        {
            public void valueChanged(KeyValueCoding object, String key)
            {
                getDocument().getUndoManager().setActionName(actionName);
            }
        }, ManagedText.TEXT_KEY);
        return text;
    }
}
